//# sourceURL=http://uMapApp/Module/ETC/ETCModule
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../../UI/Panel/PanelBlock/ListBlock"
    , "../../UI/Panel/PanelBlock/PropertiesBlock"
    , "../../UI/Panel/Item/ListItem"
    , "../../UI/Layer"
    , "./ETC"
], function (
    mhjs,
    uMap,
    uEarth,
    Module,
    FloatPanel,
    ListBlock,
    PropertiesBlock,
    ListItem,
    Layer,
    ETC
) {

    function ETCModule(option) {

        var _this = this;
        Module.call(_this, option);

        _this.title = "ETC门架";
        _this.icon = _this.app.icons.ETC;
        _this.panels = [];
        _this.items = [];

        _this.listPanel = new FloatPanel({"class": "rightPanel", "showClose": true});
        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock"});
        _this.listPanel.listBlock.title = "ETC";
        _this.listPanel.append(_this.listPanel.listBlock);

        _this.listPanel.on("close", function () {
            _this.closePanel();
        });

        _this.panels.push(_this.listPanel);

        // _this.detailPanel = new FloatPanel({"class": "leftPanel", "showClose": true});
        // _this.propertiesBlock = new PropertiesBlock({"class": "fullBlock"});
        // _this.detailPanel.append(_this.detailPanel.propertiesBlock);

        // _this.detailPanel.on("open", function () {
        //     _this.trigger("openDetail");
        // });
        //
        // _this.detailPanel.on("close", function () {
        //     _this.trigger("closeDetail");
        //     _this.unActiveAllItem();
        // });

        // _this.panels.push(_this.detailPanel);


        _this.app.ui.layout.right.append(_this.listPanel);
        // _this.app.ui.layout.left.append(_this.detailPanel);
    }

    var _class = ETCModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data"           : {
            set: function (data) {
                var _this = this;
                _this.clear();
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList"     : {
            value: function (list) {

                var _this = this;

                $.each(list, function (i, item) {

                    if (_this.objMap) {
                        item = _this.objMap.format(item);
                    }

                    item.app = _this.app;
                    item.icon = _this.icon;

                    var etc = new ETC(item);
                    _this.items.push(etc);

                    etc.listItem = new ListItem({
                        "title"   : etc.title,
                        "subtitle": etc.subtitle,
                        "icon"    : _this.icon.base64,
                        "etc"     : etc
                    });

                    etc.propertiesBlock = new PropertiesBlock();
                    etc.propertiesBlock.title = etc.title;
                    etc.propertiesBlock.map = _this.propertiesMap;
                    etc.propertiesBlock.properties = etc;

                    etc.openPop = function () {
                        etc.active();
                        _this.app.pop.target = etc;
                        _this.app.pop.content = etc.propertiesBlock;
                        _this.app.pop.position = etc.position;
                        _this.app.pop.one("close", function () {
                            etc.active(false);
                            etc.trigger("closePop");
                        });
                        _this.app.pop.open();
                    };

                    etc.closePop = function () {
                        _this.app.pop.close();
                    };

                    etc.on("click", function () {
                        _this.activeItem(etc);
                        etc.openPop();
                    });

                    etc.on("close", function (etc) {
                        if (_this.app.pop.target === etc) {
                            _this.app.pop.close();
                        }
                    });

                    // etc.open();

                    etc.listItem.on("click", function () {
                        _this.activeItem(etc);
                        etc.layer.active();

                        // _this.propertiesBlock.title = etc.title;
                        // _this.detailPanel.propertiesBlock.map = _this.propertiesMap;
                        // _this.detailPanel.propertiesBlock.properties = etc;
                        // _this.detailPanel.open();

                        // _this.app.servers.DC.requests.ETC_properties_map.send({}, function (ETC_map_json) {
                        //     _this.detailPanel.propertiesBlock.title = etc.name;
                        //     _this.detailPanel.propertiesBlock.map = ETC_map_json.data;
                        //     etc.dataObj.properties["编号"] = etc.num;
                        //     etc.dataObj.properties["名称"] = etc.name;
                        //     _this.detailPanel.propertiesBlock.properties = item.dataObj.properties;
                        //     _this.detailPanel.open();
                        // });
                    });

                    etc.listItem.on("hide", function (listItem) {
                        listItem.etc.close();
                    });

                    etc.listItem.on("show", function (listItem) {
                        listItem.etc.open();
                    });

                    _this.listPanel.listBlock.add(etc.listItem);

                });

                _this.listPanel.listBlock.filter = {
                    "param": "etc.searchKey"
                };

                _this.listPanel.listBlock.sorter = {
                    "param": {
                        "value": "etc.sortKey",
                        "label": "桩号"
                    }
                };
            }
        },
        "clear"          : {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "open"           : {
            value: function (forceReload) {
                var _this = this;
                if (forceReload || !_this.isLoaded) {
                    _this.load({}, function (data, mapData) {
                        if (mapData) {
                            _this.objMap = mapData.obj;
                            _this.propertiesMap = mapData.properties;
                        }

                        _this.data = data;

                        _this.isLoaded = true;
                        _this.open();
                    });

                } else {
                    _this.isOpened = true;
                    $.each(_this.items, function (i, item) {
                        item.open();
                    });
                    _this.trigger("open");
                    _this.trigger("toggle");
                }
            }
        },
        "openPanel"      : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close"          : {
            value: function () {
                var _this = this;
                _this.closePanel(false);
                _this.unActiveAllItem();
                $.each(_this.items, function (i, item) {
                    item.close();
                });
                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "activeItem"     : {
            value: function (item) {
                var _this = this;
                _this.unActiveAllItem();
                item.active();
                item.listItem.addClass("active");
                item.listItem.parent().animate({"scrollTop": item.listItem.dom[0].offsetTop});
            }
        },
        "unActiveAllItem": {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);
                    item.listItem.removeClass("active");
                });
            }
        }
    });

    return _class;

});